import { join } from 'path';
import { parse } from 'yaml';
import { readFileSync } from 'fs';

// 获取项目运行环境
export const getEnv = () => {
  return process.env.RUNNING_ENV;
};
export const getConfig = (type?: string) => {
  const environment = getEnv();
  const yamlPath = join(process.cwd(), `/.${environment}.yaml`);
  const file = readFileSync(yamlPath, 'utf8');
  const config = parse(file);
  if (type) {
    return config[type];
  }
  return config;
};

export const simpleUUIDGenerator = () => {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    const r = (Math.random() * 16) | 0,
      v = c == 'x' ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
};
const { APP_SERVE } = getConfig();
export const imagePath = `${APP_SERVE.domain}:${APP_SERVE.port}/files/`;
